package com.itheima.servlet;

import com.itheima.domin.Student;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;


@WebServlet("/demo4")
public class ServletDemo4 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数手动封装对象

        //1.获取所有的数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbies = req.getParameterValues("hobby");

        //2.封装学生对象
        Student stu = new Student(username,password,hobbies);

        //输出对象
        System.out.println(stu);

        System.out.println("--------------------------");


        //获取参数工具类封装对象
        Map<String, String[]> map = req.getParameterMap();
        //封装学生对象
        Student s = new Student();
       // 调用方法
      //  BeanUtils.populate(s,map);

        try {
            BeanUtils.populate(s,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

            //输出对象
        System.out.println(s);

    }
}
